package cn.picclife.mwx.manager.sys.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.picclife.mwx.manager.sys.mapper.SysMaintainHtmlMapper;
import cn.picclife.mwx.manager.sys.model.bo.DataResponse;
import cn.picclife.mwx.manager.sys.model.bo.PageCount;
import cn.picclife.mwx.manager.sys.model.bo.PageResponse;
import cn.picclife.mwx.manager.sys.model.bo.SysMaintainVUrlReq;
import cn.picclife.mwx.manager.sys.model.po.SysMaintain;
import cn.picclife.mwx.manager.sys.model.po.SysMaintainHtml;
import cn.picclife.mwx.manager.sys.model.po.SysMaintainVersionUrl;
import cn.picclife.mwx.manager.sys.service.SysMaintainHtmlService;
import cn.picclife.mwx.manager.util.enums.ResponseStatusEnum;

@Service
public class SysMaintainHtmlServiceImpl implements SysMaintainHtmlService{

	@Autowired
	private SysMaintainHtmlMapper sysMaintainHtmlMapper;
	
	/**
	 * 新增页面信息
	 * @param req
	 * @throws Exception
	 */
	public void saveSysHtml(SysMaintainVUrlReq req) throws Exception {
		
		SysMaintainHtml html = new SysMaintainHtml();
		html.setId(UUID.randomUUID().toString());
		html.setFileName(req.getFileName());
		html.setFileUrl(req.getFileUrl());
		html.setSysmaintainId(req.getSysmaintainId());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(req.getUpdateTime()!=null&&req.getUpdateTime().trim().length()>0) {
			html.setUpdateTime(sdf.parse(req.getUpdateTime()));
		}
		sysMaintainHtmlMapper.saveSysHtml(html);
	}
	
	/**
	 * 更新页面信息
	 * @param req
	 * @throws Exception
	 */
	public DataResponse updateSysHtml(SysMaintainVUrlReq req) throws Exception {
		
		DataResponse dataResponse = new DataResponse();
		SysMaintainHtml html = new SysMaintainHtml();
		html.setId(req.getId());
		List<SysMaintainHtml>list = sysMaintainHtmlMapper.querySysHtml(html);
		if(list==null||list.size()<=0) {
			dataResponse.setCode(ResponseStatusEnum.NOT_DATA.getCode());
			dataResponse.setMsg(ResponseStatusEnum.NOT_DATA.getName());
			return dataResponse;
		}
		html.setFileName(req.getFileName());
		html.setFileUrl(req.getFileUrl());
		html.setSysmaintainId(req.getSysmaintainId());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(req.getUpdateTime()!=null&&req.getUpdateTime().trim().length()>0) {
			html.setUpdateTime(sdf.parse(req.getUpdateTime()));
		}
		int count = sysMaintainHtmlMapper.updateSysHtml(html);
		if(count==0) {
			dataResponse.setCode(ResponseStatusEnum.UPDATE_FAIL_CODE.getCode());
			dataResponse.setMsg(ResponseStatusEnum.UPDATE_FAIL_CODE.getName());
			return dataResponse;
		}
		dataResponse.setCode(ResponseStatusEnum.SUCCESS_CODE.getCode());
		dataResponse.setMsg(ResponseStatusEnum.SUCCESS_CODE.getName());
		return dataResponse;
	}
	
	/**
	 * 查询页面信息
	 * @param req
	 * @throws Exception
	 */
	public PageCount querySysHtml(SysMaintainVUrlReq req) {
		
		PageResponse pageResponse = new PageResponse();
		PageCount pageCount = new PageCount();
		pageCount.setNowPage(req.getNowPage());//当前页
		pageCount.setRowNum(req.getRowNum());//每页多少条
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("sysmaintainId", req.getSysmaintainId());//系统id
		int allCount = sysMaintainHtmlMapper.queryAllCount(paramMap);
		if(0==pageCount.getRowNum()) {
			pageCount.setRowNum(10);
		}
		if(0==pageCount.getNowPage()) {
			pageCount.setNowPage(1);
		}
		int allPages = allCount / pageCount.getRowNum();

		if (allCount % pageCount.getRowNum() > 0) {
			allPages += 1;//总页数
		}

		pageCount.setAllPages(allPages);
		pageCount.setAllCount(allCount);
		int minTotal = (pageCount.getNowPage() - 1) * pageCount.getRowNum();
		int maxTotal = pageCount.getNowPage() * pageCount.getRowNum();
		pageCount.setMinTotal(minTotal);
		pageCount.setMaxTotal(maxTotal);
		pageCount.setMaxminTotal(maxTotal-minTotal);
		pageCount.setObject(paramMap);
		List<SysMaintainHtml> htmlList = sysMaintainHtmlMapper.querySysMaintainPage(pageCount);
		pageCount.setDatas(htmlList);
		return pageCount;
	}
}
